<?php

// Direct access protection
if( !defined( 'LIRIOPE' )) die( 'Direct access is not allowed.' );

/**
 * View class
 * Handles throwing to HTML
 */

load::file( load::exists( 'Obj.class.php'), TRUE );

class View extends obj {

  // @var string The controller to call
  var $_controller;

  // @var string The action to call
  var $_action;

  // @var string The page object
  var $_page;

  // @var boolean Cache flag (default FALSE)
  var $cacheEnabled = FALSE;

  /**
   * CONSTRUCTOR
   *
   * @param string $controller The name of the controller to use before rendering
   * @param string $action     The name of the method in the cotnroller to call
   */
  public function __construct( $controller, $action ) {
    global $site;
    $site = new Site();

    $this->_controller = $controller;
    $this->_action = $action;
    $file = load::exists( $controller . '/' . $action . '.php' );
    if( !$file ) throw new Exception("We can't find that view file: $file");

    $page = new Page( $file );
    $page->controller = $controller;
    $page->action = $action;
    $page->uri = uri::get();
    $page->theme = c::get('theme');
    $this->_page = &$page;
  }

  // load()
  // Render the output directly to the page or optionally return the
  // generated output to caller (like for modules or partials).
  //
  function load() {
    global $site;
    $page = &$this->_page;

    if( c::get( 'debug' )) theme::set( 'error', error::render( TRUE ));
    
    // CACHE
    // ----------
    $cache = NULL;
    $cacheModified = time();
    $cacheID = uri::md5URI();
    $cacheExpiredTime = c::get('cache.expiration', (24*60*60));

    // if cache is enabled...
    if(c::get('cache')) {
      $cacheModified = cache::modified( $cacheID );

      // ...and the cache file is newer than all of the content files...
      if( $cacheModified >= dir::modified( c::get( 'root.content' ))) {

        // ...and the cache file created time is withing the expiration time
        if(!cache::expired($cacheID, $cacheExpiredTime)) {

          $cacheData = cache::get( $cacheID, TRUE );

        }
      }
    }

    if( empty( $cacheData )) {

      // first, render the page and store the output
      $html = $page->render();

      if( $page->getTheme() !== NULL ) {
        $html = theme::load( $page->getTheme(), array( 'page'=>$page, 'content'=>$html ), TRUE );
        $html = filter::doFilters( $html );
        if( c::get( 'cache' )) { cache::set( $cacheID, (string) $html, TRUE ); }
        if( c::get( 'index' ) && http_response_code() !== 404 ) {
            index::store( uri::get(), (string) $html, (string) $html );
        } else {
            die('this was a 404, so don\'t index');
        }
      }

      // Add a clear cache button if the configuration is set to [debug]
      if( c::get('debug')) {
        $cclink = url( router::rule( 'flush' ));
        $html = preg_replace( '/<\/body>/i', '<div id="cacheBox" style="display:none;"><a href="'.$cclink.'">Clear Cache</a></div></body>', $html );
      }
    } else {
      $html = $cacheData;
    }

    // OUTPUT TO BROWSER
    echo trim( $html );
  }

}
?>
